<?php
namespace app\admin\model;
use think\Model;
use think\Loader;
use location;
class Users extends Model
{
	//注册用户信息显示
	public function index(){
		$all = input('get.all');
        $key = trim(input('get.key','','htmlspecialchars'));
        $vip = input('post.vip');
        if(!empty($vip)){
           $data = db('users')->where('vip',$vip)->order('uid desc')->paginate(15);
        }else{           
            //显示全部数据
            if($all == 1){
                $data = db('users')->select();
            }else{         
    		    $data = db('users')->where('username','like',"%".$key."%")->order('uid desc')->paginate(15);
            }
        }
		return $data;
	}
    public function addsole(){
        $data['username'] = trim(input('post.username'));
        $data['password'] = trim(md5(md5(input('post.password'))));
        $agree = input('post.agree');
        $count = db('users')->count();
        if(!$count){
            $data['pid'] = 0;
            $data['vip'] = 3;
            $data['createtime'] = date('Y-m-d H:i:s');
            $data['ip'] = request()->ip();
            if(!$agree){
                return 4;
            }else{  
              $content = '添加用户管理管理总代理';
              admin_log($content);         
              db('users')->insertGetId($data);
              //成功返回注册的用户
              return 1;
            }
            //添加总代理
           /* $data['icode1'] = getRandomString();
            $data['icode2'] = getRandomString();
            if($data['icode1'] != $data['icode2']){
                    $data['pid'] = 0;
                    $data['vip'] = 3;
                    $data['createtime'] = date('Y-m-d H:i:s');
                    $data['ip'] = request()->ip();
                    if(!$agree){
                        return 4;
                    }else{  
                      $content = '添加用户管理管理总代理';
                      admin_log($content);         
                      db('users')->insertGetId($data);
                      //成功返回注册的用户
                      return 1;
                    }
                }else{
                    //生成的邀请码相同
                    return 403;
                }*/
        }else{
           return 3; 
        }
        
    }
    
    //添加注册用户
    public function addusers(){
        $data['username'] = trim(input('post.username'));
        //判断数据库该用户名是否已经注册
        if(db('users')->field('username')->where($data)->count()){
           //用户已经存在
            return 602;
        }else{    
            $data['password'] = trim(md5(md5(input('post.password'))));
            $agree = input('post.agree');
            $code = trim(input('post.code'));
            $msg = db('users')->field('uid,icode1,icode2')->where('icode1',$code)->whereOr('icode2',$code)->find();
            $data['pid'] = $msg['uid'];
            if($code == $msg['icode1']){
                //用户填写邀请码1成为会员
                $data['vip'] = 1;
                $data['createtime'] = date('Y-m-d H:i:s');
                $data['ip'] = request()->ip();
                if(!$agree){
                        return 4;
                    }else{ 
                      $content = '添加用户管理中注册用户';
                      admin_log($content);           
                      db('users')->insertGetId($data);
                      $msg['icode1'] = " ";
                      db('users')->where('uid',$data['pid'])->update($msg);
                      //成功返回注册的用户
                      return 1;
                    }
            }elseif($code == $msg['icode2']){
                $data['vip'] = 2;
                $data['createtime'] = date('Y-m-d H:i:s');
                $data['ip'] = request()->ip();
                if(!$agree){
                    return 4;
                }else{          
                  db('users')->insertGetId($data);
                  $msg['icode2'] = " ";
                  db('users')->where('uid',$data['pid'])->update($msg);
                  //成功返回注册的用户
                  return 1;
                }
            }else{
                //不存在该邀请码
                return 604;
            }     

                //填写邀请码2成为代理
                /*$data['icode1'] = getRandomString();
                $data['icode2'] = getRandomString();
                $codes = db('users')->field('icode1,icode2')->where('icode1',$data['icode1'])->whereOr('icode1',$data['icode2'])->whereOr('icode2',$data['icode1'])->whereOr('icode2',$data['icode2'])->find();
                if(($data['icode1'] != $data['icode2']) && !isset($codes)){
                    $data['vip'] = 2;
                    $data['createtime'] = date('Y-m-d H:i:s');
                    $data['ip'] = request()->ip();
                    if(!$agree){
                        return 4;
                    }else{          
                      db('users')->insertGetId($data);
                      //成功返回注册的用户
                      return 1;
                    }
                }else{
                    //存在该邀请码或生成的邀请码相同,重新注册
                    return 603;
                }

            }else{
                //不存在该邀请码
                return 604;
            }          */ 
        }
    }

    //用户信息修改
    public function edit(){
        $data = input('post.');
        $validate = Loader::validate('Users');
        //验证不通过
        if(!$validate->check($data)){
         return ['valid'=>0,'msg'=>$validate->getError()];
        }else{
            $uid = input('param.uid');
            $data['password'] = md5(md5(input('post.password')));
            $data['updatetime'] = date('Y-m-d H:i:s');
            $datas = db('users')->where('uid',$uid)->setField($data);
            if($datas){   
               $content = '修改用户管理信息';
                admin_log($content);    
               return ['valid'=>1,'msg'=>'修改成功'];
            }else{
                //修改失败
                return ['valid'=>0,'msg'=>'修改失败'];
            }
        }
    }
}